# --------------------------------------------------------------------------------------------
# DIRECTORIES
# --------------------------------------------------------------------------------------------

BUILD_DIR   := $(CURDIR)/build
SOURCE_DIR  := $(CURDIR)/src
INCLUDE_DIR := $(CURDIR)/include

SDL_CFLAGS := $(shell sdl-config --cflags)
SDL_LDFLAGS := $(shell sdl-config --libs)

# --------------------------------------------------------------------------------------------
# EXECUTABLE
# --------------------------------------------------------------------------------------------

EXECUTABLE_NAME := gaggia

# --------------------------------------------------------------------------------------------
# REMOTE HOST
# --------------------------------------------------------------------------------------------

REMOTE_HOST  := elmyra
REMOTE_USER  := pi
REMOTE_DIR   := /home/pi


# --------------------------------------------------------------------------------------------
# COMPILER AND FLAGS
# --------------------------------------------------------------------------------------------

CC      := g++
CFLAGS  := -c -Wall -std=c++11
DFLAGS  := -D LOGGER_ON -D LOGGER_DESCRIPTION -D LOGGER_DATE_TIME
LDFLAGS :=

LIB     := -L/usr/local/lib
INC     := -I/usr/local/include -I$(INCLUDE_DIR)

# --------------------------------------------------------------------------------------------
# SOURCE AND OBJECT AGGREGATION
# --------------------------------------------------------------------------------------------

SOURCES    := $(wildcard $(SOURCE_DIR)/*.cpp)
OBJECTS    := $(patsubst $(SOURCE_DIR)/%,$(BUILD_DIR)/%,$(SOURCES:.cpp=.o))
EXECUTABLE := $(BUILD_DIR)/$(EXECUTABLE_NAME)

# --------------------------------------------------------------------------------------------
# MAIN TARGETS
# --------------------------------------------------------------------------------------------

all: pre-build main-build
pre-build:
	@mkdir -p build
main-build: $(SOURCES) $(EXECUTABLE)

# --------------------------------------------------------------------------------------------
# LINK
# --------------------------------------------------------------------------------------------

$(EXECUTABLE): $(OBJECTS) 
	@echo "Linking..."
	$(CC) $(LIB) $(LDFLAGS) $(OBJECTS) -o $@ -lpigpiod_if -lrt -lpthread -lSDL -lSDLmain -lSDL_ttf -lSDL_image

# --------------------------------------------------------------------------------------------
# COMPILE
# --------------------------------------------------------------------------------------------

$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.cpp
	@echo "Compiling..."
	$(CC) $(INC) $(DFLAGS) $(CFLAGS) $< -o $@


# --------------------------------------------------------------------------------------------
# CLEAN
# --------------------------------------------------------------------------------------------

clean:
	@echo "Cleaning..."
	@rm -f $(BUILD_DIR)/*.o $(BUILD_DIR)/*.i $(BUILD_DIR)/*.s $(BUILD_DIR)/*~ $(ALL)
	@rm -f $(EXECUTABLE)
	@echo "Done."

# --------------------------------------------------------------------------------------------

#scp $(EXECUTABLE) pi@elmyra:/home/pi
#"scp $(EXECUTABLE) $(REMOTE_USER)@$(REMOTE_HOST):$(REMOTE_DIR)"
